using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;

public abstract class Feature : ScriptableObject
{
    [Flags]
    public enum Trigger
    {
        None = 0,
        OnDead = 1 << 0,
        OnBeHit = 1 << 1,
        OnAttack = 1 << 2,
        OnVictory = 1 << 3,
        OnStart = 1 << 4,
        OnTimeUp = 1 << 5,
        Forever = 1 << 6,
        OnKill = 1 << 7,
        OnPlaced = 1 << 8
    }

    public Trigger trigger;

    public Roots originalRoots;

    [HideInInspector] public float lastSpellTime = 0;

    public float CD = 0;

    public int maxTriggerNum = -1;

    public int triggerNum = 0;

    public bool IfForever = false;

    public void Init(Pawn pawn)
    {
        if (trigger.HasFlag(Trigger.OnDead))
        {
            pawn.OnDeadEvent += DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnAttack))
        {
            pawn.OnAttackEvent += DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnBeHit))
        {
            pawn.OnBeHitEvent += DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnStart))
        {
            pawn.OnStartEvent += DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnTimeUp))
        {
            pawn.OnTimeUpEvent += DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnVictory))
        {
            pawn.OnVictoryEvent += DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnKill))
        {
            pawn.OnKillEvent += DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnPlaced))
        {
            pawn.OnKillEvent += DoEffect;
        }
    }

    public void Remove(Pawn pawn)
    {
        if (trigger.HasFlag(Trigger.OnDead))
        {
            pawn.OnDeadEvent -= DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnAttack))
        {
            pawn.OnAttackEvent -= DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnBeHit))
        {
            pawn.OnBeHitEvent -= DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnStart))
        {
            pawn.OnStartEvent -= DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnTimeUp))
        {
            pawn.OnTimeUpEvent -= DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnVictory))
        {
            pawn.OnVictoryEvent -= DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnKill))
        {
            pawn.OnKillEvent -= DoEffect;
        }
        if (trigger.HasFlag(Trigger.OnPlaced))
        {
            pawn.OnKillEvent -= DoEffect;
        }
    }

    public void Reset()
    {
        lastSpellTime = 0;
        triggerNum = 0;
    }

    public void DoEffect(FeatureEventArgs args)
    {
        if (Time.time - lastSpellTime >= CD)
        {
            lastSpellTime = Time.time;

            if (maxTriggerNum > 0)
            {
                if (triggerNum >= maxTriggerNum)
                {
                    return;
                }
            }

            triggerNum++;
            ExecuteEffect(args);
        }
    }

    public abstract void ExecuteEffect(FeatureEventArgs args);


    public abstract void CancleEffect(FeatureEventArgs args);

    [TextArea]
    public string Description;

    public virtual string GetDescription()
    {
        return"";
    }

    public string GetTriggerText()
    {
        if (trigger.HasFlag(Trigger.OnDead))
        {
            return"死亡时，";
        }
        if (trigger.HasFlag(Trigger.OnAttack))
        {
            return"攻击时，";
        }
        if (trigger.HasFlag(Trigger.OnBeHit))
        {
            return"受击时，";
        }
        if (trigger.HasFlag(Trigger.OnStart))
        {
            return"开始时，";
        }
        if (trigger.HasFlag(Trigger.OnTimeUp))
        {
            return"每"+CD+"秒，";
        }
        if (trigger.HasFlag(Trigger.OnVictory))
        {
            return"胜利时，";
        }
        if (trigger.HasFlag(Trigger.OnKill))
        {
            return"击杀时，";
        }
        if (trigger.HasFlag(Trigger.OnPlaced))
        {
            return"放置时，";
        }
        return "";
    }
}


